<?php
/**
 * 主页布局
 */
namespace Admin\Controller;
use Think\Controller;
class HomePageLayoutController extends Controller {

	public function index(){}
	

	//编辑自定义主页页面
	public function layout(){
		
		$homepage_id = I('id');

		$merchant_id = 1;

		if(!empty($homepage_id)){
			
			$result = M('LayoutList')->alias('a')
						->join('LEFT JOIN __LAYOUT_HTML__ AS b ON a.content_id = b.id')
						->where(array('a.homepage_id'=>$homepage_id,'a.merchant_id'=>$merchant_id))
						->field('a.template_title,b.content')->find();

			if(empty($result)){
				$this->error('服务器提了一个问题！~');
			}

			$homepage_content = json_decode($result['content'],1); 

			$html = $homepage_content['html'];

			$carousel = $homepage_content['carousel'];

		}

		$this->assign(array(
			'html' => htmlspecialchars_decode($html),
			'carousel' => json_encode($carousel),
			'result' => $result,
			'homepage_id' => $homepage_id,
			'merchant_id' => $merchant_id,
		));

		$this->display('layout');
	}


	//预览页面
	public function preview(){

		$homepage_id = I('id');

		$merchant_id = 1;

		if(empty($homepage_id)){
				$this->error('服务器提了一个问题！~');
			}

		$result = M('LayoutList')->alias('a')
					->join('LEFT JOIN __LAYOUT_HTML__ AS b ON a.content_id = b.id')
					->where(array('a.homepage_id'=>$homepage_id,'a.merchant_id'=>$merchant_id))
					->field('a.template_title,b.content')->find();

		if(empty($result)){
			$this->error('服务器提了一个问题！~');
		}

		$homepage_content = json_decode($result['content'],1); 

		$html = $homepage_content['html'];

		$this->assign(array(
			'html' => htmlspecialchars_decode($html),
		));

		$this->display('preview');
	}


	//保存html
	public function SaveHtml(){
		if ( IS_AJAX === false ) {
			$this->error('ERROR');
		}

		$homepage_content = I('html');    //商家模板内容
		$homepage_content = json_encode($homepage_content);
		$homepage_id = I('homepage_id');  //模板ID
		$merchant_id = I('merchant_id');  //商家ID
		$template_title = I('template_title');
		

		if( empty($homepage_id) ){
			
			//添加新模板
			$content_id = M('LayoutHtml')->add(array('content'=>$homepage_content));
			$SaveData = array(
				'template_title' => $template_title,
				'content_id' => $content_id,
				'merchant_id' => $merchant_id,
				'is_update' => 1,
				'is_default' => 0,
				'createtime' => NOW_TIME,
			);
			$homepage_id = M('LayoutList')->add($SaveData);
			
			if($content_id > 0 && $homepage_id > 0){
				$returnArr = array('status' => 0,'msg' => 'OK');
				$returnArr['url'] = U( 'HomePageLayout/layout', array('id'=>$homepage_id) );
				$returnArr['preview_url'] = U( 'HomePageLayout/preview', array('id'=>$homepage_id) );
				
			}else{
				$returnArr = array('status' => 1,'msg' => 'ERROR');
			}

			

		}else{

			//更新模板
			
			$content_id = M('LayoutList')->where(array('homepage_id'=>$homepage_id))->getField('content_id');
			
			$html_num_rows = M('LayoutHtml')->where(array('id'=>$content_id))->save(array('content'=>$homepage_content));

			$SaveData = array(
				'template_title' => $template_title,
				'is_update' => $html_num_rows,
				'createtime' => NOW_TIME,
			);

			$list_num_rows = M('LayoutList')->where(array('homepage_id'=>$homepage_id))->save($SaveData);

			if($list_num_rows === 1 || $html_num_rows === 1){
				$returnArr = array('status' => 0,'msg' => 'OK');
				$returnArr['url'] = U( 'HomePageLayout/layout', array('id'=>$homepage_id) );
				$returnArr['preview_url'] = U( 'HomePageLayout/preview', array('id'=>$homepage_id) );
			}else{
				$returnArr = array('status' => 1,'msg' => 'ERROR');
			}

		}
		
		$this->ajaxReturn($returnArr);
		
	}


}
